package io.xccit.order;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author CH_ywx
 * @version 1.0
 * @create 2025/5/17
 * <p></p>
 */
@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

    /**
     * 启动成功后执行的一次性任务
     * @param nacosConfigManager 自动从容器中拿出的配置管理器(原本没有参数,加上之后会自动从容器中拿)
     * @return
     */
    @Bean
    ApplicationRunner applicationRunner(NacosConfigManager nacosConfigManager){
        return args -> {
            System.out.println("Order服务启动成功");
            ConfigService configService = nacosConfigManager.getConfigService();
            //参数值参照Nacos配置文件的属性来填写 配置文件ID 配置文件所属组
            configService.addListener("service-order.properties", "DEFAULT_GROUP", new Listener() {
                @Override
                public Executor getExecutor() {
                    //返回一个固定4个线程的线程池(随意,不一定是4)
                    return Executors.newFixedThreadPool(4);
                }

                @Override
                public void receiveConfigInfo(String s) {
                    System.out.println("监听到配置文件更新: " + s);
                }
            });
        };
    }
}
